package javaNIO1.channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.*;

/**
 * Created with Intel IDEA.
 *
 * @Auther: gjt
 * @Date: 2024/7/7 15:49
 */
public class channelCreate {

    /**
     * 一般 通道有两种 Socket 和 FileSocket
     * SocketChannel 可以直接创建通过工厂类 而文件通道 只能通过在一个打开的RandomAccessFile,FileInoutStream,FileOutOutStream 对象上调用getChannel() 来获取
     * @param args
     */
    public static void main(String[] args) throws IOException {

        // 打开通道
        SocketChannel open = SocketChannel.open();
        open.connect(new InetSocketAddress("152.136.138.218",80));
        ServerSocketChannel open1 = ServerSocketChannel.open();
        open1.socket().bind(new InetSocketAddress("152.136.138.218",80));
        DatagramChannel open2 = DatagramChannel.open();

        RandomAccessFile r = new RandomAccessFile("D:\\1.png", "r");
        FileChannel channel = r.getChannel();
        FileInputStream fileInputStream = new FileInputStream("D:\\1.png");
        FileChannel channel1 = fileInputStream.getChannel();

        copyChannel();


    }


    /**
     * 通道数据复制到另一个通道中
     */

    private static void copyChannel () throws IOException{
        ReadableByteChannel readableByteChannel = Channels.newChannel(System.in);
        WritableByteChannel writableByteChannel = Channels.newChannel(System.out);

        /**
         *  方式1
         */
        // 创建字节缓冲区
        ByteBuffer buffer = ByteBuffer.allocateDirect(10 * 1024);
        while(readableByteChannel.read(buffer) != -1){
            buffer.flip();

            writableByteChannel.write(buffer);

            buffer.compact();
        }

        buffer.flip();
        while(buffer.hasRemaining()){
            writableByteChannel.write(buffer);
        }


        // 方式2
        while(readableByteChannel.read(buffer) != -1){
            buffer.flip();
            // 保证缓冲区是满的状态
            while (buffer.hasRemaining()){
                writableByteChannel.write(buffer);
            }
            // 清空缓冲区
            buffer.clear();

        }

        readableByteChannel.close();
        writableByteChannel.close();
    }


    /**
     * 通道数据复制另一个通道中
     */

    private static void copyChannel1 () throws IOException{
        ReadableByteChannel readableByteChannel = Channels.newChannel(System.in);
        WritableByteChannel writableByteChannel = Channels.newChannel(System.out);

        // 方式1：
        ByteBuffer buffer = ByteBuffer.allocateDirect(10 * 1024);
        while (readableByteChannel.read(buffer) != -1){
            // 翻转：位置为0 上界到达第一个没有元素的位置
            buffer.flip();

            writableByteChannel.write(buffer);


            // 如果缓冲区为空 同样也是做清除
            buffer.compact();
        }
        buffer.flip();

        // 确保缓冲区是满的状态
        while(buffer.hasRemaining()){
            writableByteChannel.write(buffer);
        }

        // 方式2
        while (readableByteChannel.read(buffer) != -1){
            buffer.flip();
            while (buffer.hasRemaining()){
                writableByteChannel.write(buffer);
            }

            buffer.clear();
        }

        // 关闭通道
        readableByteChannel.close();
        writableByteChannel.close();

    }
}
